package com.fs.alert.controller;

import com.fs.alert.domain.dto.DispatchRequestDTO;
import com.fs.alert.domain.po.Alert;
import com.fs.alert.domain.vo.AlertStatsVO;
import com.fs.api.domain.vo.AlertDetailVO;
import com.fs.api.enumeration.AlertStatus;
import com.fs.api.enumeration.AlertType;
import com.fs.alert.service.IAlertGeneratorService;
import com.fs.alert.service.IAlertService;
import com.fs.common.domain.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Api(tags = "警情相关接口")
@RestController
@RequestMapping("/alerts")
@RequiredArgsConstructor
public class AlertController {
    private final IAlertService alertService;
    private final IAlertGeneratorService alertGeneratorService;

    @ApiOperation("获取警情列表")
    @GetMapping
    public Result getAlerts(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) AlertType alertType,
            @RequestParam(required = false) AlertStatus status,
            @RequestParam(required = false) String alertNo,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        return Result.ok(alertService.getAlerts(current, size, alertType, status, alertNo, startTime, endTime));
    }

    @ApiOperation("实时报警信息")
    @GetMapping("/dashboard")
    public Result getDashboardList() {
        return Result.ok(alertService.getDashboardList());
    }


    @ApiOperation("获取警情详情")
    @GetMapping("/{id}")
    public Result getAlertDetail(@PathVariable Long id,
                                                @RequestParam(required = false, defaultValue = "true") boolean withOthers) {
        return Result.ok(alertService.getAlertDetail(id, withOthers));
    }

    @ApiOperation("获取警情统计信息")
    @GetMapping("/statistics")
    public Result getAlertStatistics() {
        return Result.ok(alertService.getAlertStatistics());
    }

    @ApiOperation("获取警情分类统计")
    @GetMapping("/stats")
    public Result getAlertTypeStats() {
        return Result.ok(alertService.getAlertTypeStats());
    }

    @ApiOperation("饼图")
    @GetMapping("/distribution")
    public Result getAlertTypeDistribution(
            @RequestParam(defaultValue = "month") String timeRange) {
        return Result.ok(alertService.getAlertTypeDistribution(timeRange));
    }

    @ApiOperation("获取警情趋势分析")
    @GetMapping("/trend")
    public Result getAlertTrend(
            @RequestParam(required = false) String alertType) {
        return Result.ok(alertService.getAlertTrend(alertType));
    }

    @ApiOperation("机器人分配任务")
    @PostMapping("/dispatch")
    public Result dispatch(@RequestBody DispatchRequestDTO request) {
        alertGeneratorService.dispatchTask(request);
        return Result.ok();
    }

    @ApiOperation("机器人完成任务")
    @PutMapping("/{id}/complete")
    public Result complete(@PathVariable("id") Long alertId) {
        alertGeneratorService.completeTask(alertId);
        return Result.ok();
    }


    @ApiOperation("获取警情统计数据")
    @GetMapping("/summary")
    public Result getAlertStats() {
        AlertStatsVO stats = new AlertStatsVO();
        stats.setTotalAlertCount(alertService.getTotalAlertCount());
        stats.setUrgentAlertPercentage(String.format("%.1f%%", alertService.getUrgentAlertPercentage()));
        stats.setMostCommonAlertType(alertService.getMostCommonAlertType());
        stats.setAverageResponseTime(String.format("%.1f分钟", alertService.getAverageResponseTime()));
        return Result.ok(stats);
    }

    @ApiOperation("获取近六个月关键指标趋势")
    @GetMapping("/stat-trend")
    public Result getAlertTrend() {
        return Result.ok(alertService.getAlertStatsTrend());
    }
}
